package cosplay.Servlet;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cosplay.DAO.UserDAOImpl;
public class VerifyUserName extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8157226207079971686L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		UserDAOImpl userdao=(UserDAOImpl) context.getAutowireCapableBeanFactory().getBean("userdao");;
		String username=req.getParameter("username");
		String rcod=req.getParameter("cod");
		String cod=(String) req.getSession().getAttribute("rand");
		int i=userdao.verify(username);
		String message;
		String usable="";
		String equal="false";
		switch (i)
		{
		case 0:
			message="Username is usable";
			usable="true";
			break;
		case 1:
			message="Username is illegal";
			usable="false";
			break;
		case 2:
			message="Username is already in use";
			usable="false";
			break;
		default:
			message="Unknown error, please try again later";
			usable="false";
		};
		if(rcod.equalsIgnoreCase(cod))
		{
			equal="true";
		}
		resp.addHeader("usable", usable);
		resp.addHeader("message", message);
		resp.addHeader("equal", equal);
		System.out.println("verified.");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

}
